<template>
  <div class="wh100">
    <!-- <el-upload
      class="avatar-uploader"
      action="https://jsonplaceholder.typicode.com/posts/"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
    >
      <img v-if="imageUrl" :src="imageUrl" class="avatar" />
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload> -->
    <el-form label-position="top" ref="form" :model="form" label-width="80px">
      <el-row :gutter="24">
        <el-col :span="24">
          <el-form-item
            label="用户名"
            prop="nickname"
            :rules="[
              {
                required: true,
                message: '请输入用户名',
                trigger: 'blur',
              },
            ]"
          >
            <el-input v-model="form.nickname"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item
            label="旧密码"
            prop="currPassword"
            :rules="[
              {
                required: true,
                message: '请输入密码',
                trigger: 'blur',
              },
            ]"
          >
            <el-input v-model="form.currPassword" type="password"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item
            label="新密码"
            prop="password"
            :rules="[
              {
                required: true,
                message: '请输入新密码',
                trigger: 'blur',
              },
            ]"
          >
            <el-input v-model="form.password" type="password"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="24" style="text-align: right">
          <el-form-item>
            <div class="btn">
              <div class="cancel">
                <el-button @click="cancelModel()">取消</el-button>
              </div>
              <div class="preservation">
                <el-button type="primary" @click="onSubmit('form')">确认</el-button>
              </div>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { updateAdmin } from "@/api/admin";
export default {
  props: {
    adminInfo: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      imageUrl: "",
      form: {
        currPassword: "",
        nickname: "",
        password: "",
      },
      loading: false,
    };
  },

  mounted() {
    console.log(this.adminInfo);
  },

  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          this.loading = true;
          let res = await updateAdmin({
            avatar: this.imageUrl,
            currPassword: this.form.currPassword,
            nickname: this.form.nickname,
            password: this.form.password,
          });

          if (res.code == 200) {
            this.loading = false;
            this.$message({
              message: "更改成功",
              type: "success",
            });
            this.$emit("onClose", {
              isClose: true,
            });
          }
        }
      });
    },

    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      console.log(file);
      const isJPG = file.type === "image/jpeg" || file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG,PNG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

    cancelModel() {
      this.$emit("onClose", {
        isClose: false,
      });
    },
  },
};
</script>

<style lang="scss">
.btn {
  display: flex;
  gap: 20px;
  justify-content: center;

  .cancel {
    ::v-deep .el-button {
      width: 146px;
      border: 1px solid rgba(92, 251, 255, 1);
      background-color: rgba(92, 251, 255, 0);
      color: rgba(92, 251, 255, 1);
    }
  }

  .preservation {
    ::v-deep .el-button {
      width: 146px;
      border: 1px solid rgba(92, 251, 255, 1);
      background-color: rgba(92, 251, 255, 1);
      color: rgba(23, 24, 26, 1);
    }
  }
}
</style>